/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-12-15
*/
package com.rzico.scenic.service;

import com.rzico.base.BaseMapper;
import com.rzico.base.CommResult;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.basics.entity.Member;
import com.rzico.exception.CustomException;
import com.rzico.scenic.entity.ScenicFavorites;
import com.rzico.scenic.entity.ScenicSpot;
import com.rzico.scenic.model.ScenicSpotVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rzico.scenic.mapper.ScenicFavoritesMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 * 我收藏的景区业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class ScenicFavoritesService extends BaseServiceImpl<ScenicFavorites, String> {

    @Autowired
    private ScenicFavoritesMapper scenicFavoritesMapper;


    @Autowired
    private ScenicSpotService scenicSpotService;



    @Override
    public BaseMapper<ScenicFavorites, String> getMapper() {
        return scenicFavoritesMapper;
    }


    /**
     * 添加景点喜欢人数(收藏人数)
     * @param scenicFavorites
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public ScenicFavorites addLove(ScenicFavorites scenicFavorites){
        int affectCount = super.insertUseGeneratedKeys(scenicFavorites);
        if (affectCount <= 0) {
            throw new CustomException("添加收藏人数错误");
        }
        Map<String,Object> updateParams = new HashMap<>();
        updateParams.put("id", scenicFavorites.getScenicSpotId());
        updateParams.put("value", 1);
        int addNum = scenicSpotService.addLove(updateParams);
        if (addNum <= 0) {
            throw new CustomException("添加收藏人数错误");
        }
        ScenicFavorites result = super.findById(scenicFavorites.getId());
        return result;
    }

    /**
     * 查询用户收藏的景区
     * @param params
     * @return
     */
    public List<ScenicSpotVo> selectFavoriteScenicList(Map<String, Object> params){
        return scenicFavoritesMapper.selectFavoriteScenicList(params);
    }

    /**
     * 批量删除旅游景区收藏
     * @param ids
     * @param member
     */
    @Transactional(rollbackFor = Exception.class)
    public  void deleteMyFavoriteByIds(List<Long> ids, Member member) {
        //一次性查询所有要删的数据
        Map<String,Object> params = new HashMap<>();
        params.put("memberId", member.getId());
        if (ids != null && ids.size() != 0) {
            //假ids
            params.put("scenicIds", ids);
        }
        //根据旅游Id查询除全部旅游数据
        List<ScenicFavorites> scenicFavorites = selectList(params);

        //景区收藏表的id加入list
        List<Long> scenicFavoritesIds = null;
        List<Long> scenicSpotIds = null;
        if (scenicFavorites != null && scenicFavorites.size() != 0) {
            scenicFavoritesIds = new ArrayList<>();
            scenicSpotIds = new ArrayList<>();
            for (ScenicFavorites scenicFavorite : scenicFavorites) {
                scenicFavoritesIds.add(scenicFavorite.getId());
                scenicSpotIds.add(scenicFavorite.getScenicSpotId());
            }
        }
        //真ids
        params.put("scenicIds", scenicSpotIds);
        //收藏表 ids删除对应的数据
        int i = deleteByIds(scenicFavoritesIds.toArray());
        if (i <= 0) {
            throw new CustomException("删除收藏人数错误");
        }
        //旅游景区喜欢人数--
        Integer integer = scenicSpotService.decLvoe(params);
    }

    public boolean getIsFavorite(Map<String, Object> params) {
        List<ScenicFavorites> scenicFavorites = scenicFavoritesMapper.selectList(params);
        if (scenicFavorites == null || scenicFavorites.size() == 0) {
            return false;
        }
        return true;
    }
}
